// TextStringManager.cpp: implementation of the CTextStringManager class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "TextStringManager.h"
#include "StringCheck.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CTextStringManager theTextStringManager;
CTextStringManager* CSingletonT<CTextStringManager>::s_pObject = NULL;

CTextStringManager::CTextStringManager()
{

}

CTextStringManager::~CTextStringManager()
{

}

void ChangePMLString(std::string& str,const char* pString)
{
	if(*pString != '"')
	{
		str = pString;
		return;
	}
	int iLen = strlen(pString)-1;
	if(iLen > 0 && pString[iLen] == '"')
	{
		str.resize(iLen-1);
		memcpy((char*)str.c_str(), pString+1, iLen-1);
	}
	else
		str = pString + 1;
}
/*
LPCTSTR	CTextStringManager::GetDefineString(DWORD dwID)
{
	mapDWLPCTSTR::iterator itFind = m_DefineStrings.find(dwID);
	if ( itFind == m_DefineStrings.end())
		return NULL;

	return (*itFind).second;
}

void CTextStringManager::LoadDefineString(LPCTSTR pFileName, IFileManager *pFM)
{
	HANDLE hf = pFM->CreateFile( pFileName, GENERIC_READ ); 
	if ( hf == INVALID_HANDLE_VALUE)
		return;
	
	DWORD size = pFM->GetFileSize( hf, 0 );
	char *dataItem = new char[size+1];
	dataItem[size] = 0;
	DWORD dw;
	pFM->ReadFile( hf, dataItem, size, &dw );
	pFM->CloseHandle(hf);

	CDocumentProcess doc;
	doc.SetDocument(dataItem,size);
	CStringProcess *pStr;
	std::string str2;
	CStringEnd sEnd, sEnd2;
	sEnd << '\t';
	sEnd2 << ',';

	DWORD dwID;
	std::string str;
	BOOL bNoUseItem = FALSE;
	while(pStr = doc.GetValidLine())
	{
		pStr->GetString( &str2, &sEnd );
		dwID = atoi(str2.c_str());
		
		pStr->GetString( &str2, &sEnd );
		char* pBuf = m_MemBufManager.GetStringBuffer(str2.length());
		memcpy(pBuf, str2.c_str(), str2.length());

		VERIFY ( m_DefineStrings.find(dwID) == m_DefineStrings.end() );
		m_DefineStrings.insert ( mapDWLPCTSTR::value_type( dwID, (LPCTSTR)pBuf ) );
	}
}
*/

void CTextStringManager::LoadFromFile( char *pFileName , DWORD	dwLanguageType, IFileManager*	pIFileMgr)
{
	HANDLE hf;
	DWORD sizeFile;
	if(pIFileMgr)
	{
		hf = pIFileMgr->CreateFile( pFileName, GENERIC_READ ); 
		sizeFile = pIFileMgr->GetFileSize( hf, 0 );
	}
	else
	{
		hf = ::CreateFile( pFileName,GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,NULL  );
		sizeFile = ::GetFileSize(hf, 0);
	}

	char *data = new char[sizeFile];

	DWORD dw;
	if(pIFileMgr)
	{
		pIFileMgr->ReadFile( hf, data, sizeFile, &dw );
	}
	else
	{
		::ReadFile(hf, data, sizeFile, &dw, NULL);
	}
		

	int size2 = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR) (data+2),
		sizeFile/2-1, 0, 0, NULL, NULL );
	char *data2 = new char[size2+1];
	WideCharToMultiByte(CP_ACP, 0, (LPCWSTR) (data+2),
		sizeFile/2-1, data2, size2, NULL, NULL );
	data2[size2] = 0;
	delete [] data;

	

	CDocumentProcess doc(false);
	doc.SetDocument(data2,size2);

	CStringProcess *pStr;
	std::string str;

	CStringEnd sEndText;
	sEndText << (char)NULL << '\t' << '\r';

	int nColumnIndex = 0;
	switch ( dwLanguageType )
	{
	case LANGUAGE_KOREAN:
		nColumnIndex = 1;
		break;
	case LANGUAGE_ENGLISH:
		nColumnIndex = 4;		
		break;
	default:
		ASSERT(false);
		break;
	}

	int nCounter = 0;
	while(pStr = doc.GetValidLine())
	{
		nCounter++;
		
		pStr->GetString(&str,&sEndText);
		std::string id = str;

		pStr->SearchContinue('\t', true);

		for(int i = 0; i < nColumnIndex; i++)
		{
			pStr->SearchContinue('\t', true);
			if(pStr->GetEnd())
				break;
		}

		std::string str2;
		if(!(pStr->GetEnd()))
		{
			pStr->GetString(&str,&sEndText,false);
			ChangePMLString(str2, str.c_str());
		}
		else
		{
			str2 = "";
		}

		thmstrstrit	itPrev = m_hmstrstr.find(id);
		if( itPrev != m_hmstrstr.end() )
		{
			char szBuff[256];
			SF_sprintf( szBuff, sizeof(szBuff), "%s duplicated!", id.data() );
			::MessageBox( NULL, szBuff, "Error", MB_ICONERROR );
			VERIFY( FALSE );
		}
		
		m_hmstrstr.insert( thmstrstrv( id, str2 ) );
	}

	delete [] data2;
	if(pIFileMgr)
	{
		pIFileMgr->CloseHandle( hf );
	}
	else
	{
		::CloseHandle( hf )	;
	}
}

void CTextStringManager::ReleaseData()
{
	m_hmstrstr.clear();
}

static std::string g_strNULL = "";

std::string &CTextStringManager::GetText( const std::string &strid )
{
	if(!strid.size())
		return g_strNULL;
	thmstrstrit it = m_hmstrstr.find( strid );
	if(it == m_hmstrstr.end()) 
		return g_strNULL;
#ifdef _DEBUG
	std::string strTemp = (*it).second;
#endif
	return (*it).second;
}

std::string &CTextStringManager::GetText( char *pstrid )
{
	if(!pstrid)
		return g_strNULL;
	std::string strid = pstrid;
	thmstrstrit it = m_hmstrstr.find( strid );
	if(it == m_hmstrstr.end()) 
		return g_strNULL;
	return (*it).second;
}

std::string	CTextStringManager::EraseQuotationForMultiLine(char*	pChar)
{
	std::string	str = pChar;
	if(str.find('\n') != -1)
	{
		if((*(str.data())) == '"')
			str = str.substr(1,str.length()-2);
	}	
	return str;	
}